/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TempoTotalFunc.java
 *
 * Created on 05/12/2011, 10:40:21
 */
package proj5labbd.graphics.relatorios;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import oracle.jdbc.OracleTypes;
import proj5labbd.DBInterface;

/**
 *
 * @author yole
 */
public class TempoTotalFunc extends javax.swing.JFrame {

    private final int width = 700;
    private final int height = 700;
    /** Creates new form TempoTotalFunc */
    private DBInterface dbInterface;
    private String nome = "Rádio e Televisão Bandeirantes de MG LTDA";
    public TempoTotalFunc(DBInterface dbInterface) {
        try {
            initComponents();
            this.setDefaultCloseOperation(TempoTotalFunc.DISPOSE_ON_CLOSE);
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((dim.width-width)/2,(dim.height-height)/2,width,height);
            this.dbInterface = dbInterface;
            setupTable();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Não foi possível gerar o relatório.");
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtbTempo = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jtbTempo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbTempo);

        jLabel1.setText("Tempo de trabalho dos funcionários");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(139, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtbTempo;
    // End of variables declaration//GEN-END:variables
   
    /*
     * Método para manipular a função tempo_total e exibir seu retorno
     */
    private void setupTable() throws SQLException {
        CallableStatement callstmt = dbInterface.getConn().prepareCall("{call ? := tempo_total(?,?)}");
        Statement stmt = dbInterface.getConn().createStatement();
        callstmt.registerOutParameter(1, OracleTypes.INTEGER);
        callstmt.setString(3, nome);
        String [] params = new String[]{"funcionario.idFu=trabalho.idFu","trabalho.idDe=departamento.idDe","departamento.cnpjEm=emissora.cnpjEm",
                                           "emissora.nomeCompletoEm='"+nome+"'"};
        String s[][] = dbInterface.view(new String[]{"funcionario.idFu","funcionario.nomeCompletoFu"}, params, new String[]{"funcionario","trabalho","departamento","emissora"});
        
        int value;
        
        String result[][] = new String[s.length][s.length+1];
                
        for ( int i=0; i < s.length; i++ ) {
            
            callstmt.setInt(2,Integer.valueOf(s[i][0]));
            callstmt.execute();
            value = callstmt.getInt(1);
            
            for ( int j=0; j < s[i].length; j++) {
                result[i][j] = s[i][j];
            }
            
            result[i][s[i].length] = String.valueOf(value)+" Dias";
        }
        
        
        DefaultTableModel model = new DefaultTableModel(result,
                new String[]{"ID","Nome","Tempo de trabalho"});
        
        jtbTempo.setModel(model);
    }
}
